﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Service.Presentation.WinForm;

namespace QuanLyNhapXuat.Presentation.WinForm
{
    public partial class FormMain : Telerik.WinControls.UI.RadForm
    {
        private static FormMain doituong = null;
        public static FormMain TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new FormMain();
            return doituong;
        }
        public FormMain()
        {
            InitializeComponent();            
        }
        private string MaNV_frmMain;
        private void radButtonElement2_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frmQuanLiTaiKhoan"] == null)
            {
                frmQuanLiTaiKhoan f_QLTK = frmQuanLiTaiKhoan.TaoDoiTuong();
                f_QLTK.StartPosition = FormStartPosition.CenterScreen;
                f_QLTK.Width = 536;
                f_QLTK.Height = 349;
                f_QLTK.Show();
            }
            else
            {
                Application.OpenForms["frmQuanLiTaiKhoan"].Activate();
                Application.OpenForms["frmQuanLiTaiKhoan"].Height = 349;
                Application.OpenForms["frmQuanLiTaiKhoan"].Width = 536;
                Application.OpenForms["frmQuanLiTaiKhoan"].Show();
                
            }
        }
       
        private void radButtonElement5_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frm_KhachHang"] == null)
            {
                frm_KhachHang kh = frm_KhachHang.TaoDoiTuong();
                kh.StartPosition = FormStartPosition.CenterScreen;
                kh.Width= 573;
                kh.Height = 263;
                kh.Show();
            }
            else
            {
                Application.OpenForms["frm_KhachHang"].Activate();
                Application.OpenForms["frm_KhachHang"].Width = 573;
                Application.OpenForms["frm_KhachHang"].Height= 263;
                Application.OpenForms["frm_KhachHang"].Show();
            }
        }

        private void radButtonElement4_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frmGiaoHang"] == null)
            {
                frmGiaoHang giaohang = frmGiaoHang.TaoDoiTuong();
                giaohang.MdiParent = this;
                giaohang.WindowState = FormWindowState.Maximized;
                giaohang.MaNV = MaNV_frmMain;                              
                giaohang.Show();
            }
            else
            {
                Application.OpenForms["frmGiaoHang"].Activate();
                Application.OpenForms["frmGiaoHang"].WindowState = FormWindowState.Maximized;
                frmGiaoHang.kichhoat = true;
                Application.OpenForms["frmGiaoHang"].Show();                
            }
        }

        private void radButtonElement11_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frmThongKeChiPhi"] == null)
            {
                frmThongKeChiPhi tkcp = frmThongKeChiPhi.TaoDoiTuong();
                tkcp.Width= 974;
                tkcp.Height = 557;
                tkcp.WindowState = FormWindowState.Normal;
                tkcp.Show();
            }
            else 
            {
                Application.OpenForms["frmThongKeChiPhi"].Activate();
                Application.OpenForms["frmThongKeChiPhi"].Width= 974;
                Application.OpenForms["frmThongKeChiPhi"].Height = 557;
                Application.OpenForms["frmThongKeChiPhi"].Show();
            }
        }

        private void radButtonElement8_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frmTimKiem"] == null)
            {
                frmTimKiem tk = new frmTimKiem();
                tk.Show();
            }
            else
            {
                Application.OpenForms["frmTimKiem"].Activate();
                Application.OpenForms["frmTimKiem"].Show();
            }
        }
        public static DataTable dt_thu;
        private void FormMain_Load(object sender, EventArgs e)
        {
            linkLabel1.Visible = false;
            radLabelElement2.Text = "";
            radLabelElement4.Text = "";
            Frm_DangNhap dn = Frm_DangNhap.TaoDoiTuong();
            dn.StartPosition = FormStartPosition.CenterScreen;
            dn.MdiParent = this;
            dn.Show();
            dt_thu = new DataTable();
            dt_thu.Columns.Add("ngaychi", typeof(DateTime));
            dt_thu.Columns.Add("tencp", typeof(string));
            dt_thu.Columns.Add("diengiai", typeof(string));
            dt_thu.Columns.Add("sotien", typeof(float));
        }

        private void radRibbonBarGroup11_Click(object sender, EventArgs e)
        {

        }


        private void radButtonElement3_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frmNhapHang"] == null)
            {
                frmNhapHang nhaphang = frmNhapHang.taodoituong();
                nhaphang.MdiParent = this;
                nhaphang.MaNV_frmNhapHang = MaNV_frmMain;
                nhaphang.Show();
            }
            else
            {
                Application.OpenForms["frmNhapHang"].Activate();
                Application.OpenForms["frmNhapHang"].Show();
            }
        }

        private void radButtonElement6_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frm_SaoLuu_PhucHoiDuLieu"] == null)
            {
                frm_SaoLuu_PhucHoiDuLieu bkrs = frm_SaoLuu_PhucHoiDuLieu.TaoDoiTuong();
                bkrs.StartPosition = FormStartPosition.CenterScreen;
                bkrs.ShowDialog();
            }
            else
            {
                Application.OpenForms["frm_SaoLuu_PhucHoiDuLieu"].Activate();
                Application.OpenForms["frm_SaoLuu_PhucHoiDuLieu"].Show();
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Application.OpenForms["frmDoiMK"] == null)
            {
                frmDoiMK doimk = frmDoiMK.TaoDoiTuong();
                doimk.Width= 392;
                doimk.Height = 210;
                doimk.Show();
            }
            else
            {
                Application.OpenForms["frmDoiMK"].Activate();
                Application.OpenForms["frmDoiMK"].Width =392;
                Application.OpenForms["frmDoiMK"].Height = 210;
                Application.OpenForms["frmDoiMK"].Show();
            }
        }

        private void ribbonTab4_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frmTimKiem"] == null)
            {
                frmTimKiem timkiem = frmTimKiem.TaoDoiTuong();
                timkiem.MdiParent = this;
                timkiem.Show();
            }
            else
            {
                Application.OpenForms["frmTimKiem"].Activate();
                Application.OpenForms["frmTimKiem"].Show();
            }
        }

        private void radButtonElement1_Click(object sender, EventArgs e)
        {
            
            if (radRibbonBarGroup1.Text == "Đăng Xuất")
            {
                if (MessageBox.Show("Bạn muốn đăng xuất ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Còn form nào là form con đang mở thì đóng frm đó
                    foreach (Form item in this.MdiChildren)
                    {
                        Application.OpenForms[item.Name].Close();
                    }

                    int vitri = Application.OpenForms.Count;//Lay số lượng các frm còn lại :

                    //Các form còn lại này sẽ không phải là frm con , đoạn code sau đây sẽ đóng các frm đó lại
                    if (Application.OpenForms != null)
                    {
                        for (int i = vitri-1; i>=0; i--)
                        {


                            if (Application.OpenForms[i] != Application.OpenForms["FormMain"])
                            {
                               
                                Application.OpenForms[i].Close();

                            }
                        }
                        //Application.OpenForms[vitri].Close();
                    }
                          
                    radButtonElement1.Image = imageList1.Images[0];
                    radRibbonBarGroup1.Text = "Đăng Nhập";
                    radLabelElement2.Text = "";
                    radLabelElement4.Text = "";
                }
            }
            else
            {
                Frm_DangNhap dangnhap = Frm_DangNhap.TaoDoiTuong();
                dangnhap.MdiParent = this;
                dangnhap.Show();

            }
        }

        private void radButtonElement10_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frmThongKeNhapXuat"] == null)
            {
                frmThongKeNhapXuat nhapxuat = frmThongKeNhapXuat.TaoDoiTuong();
                nhapxuat.MdiParent = this;
                nhapxuat.Show();
            }
            else
            {
                Application.OpenForms["frmThongKeNhapXuat"].Activate();
                Application.OpenForms["frmThongKeNhapXuat"].Show();
            }
        }

        private void radButtonElement7_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["frm_NhanVien"] == null)
            {
                frm_NhanVien nhanvien = frm_NhanVien.TaoDoiTuong();
                nhanvien.Width =663;
                nhanvien.Height= 246;
                nhanvien.Show();
            }
            else
            {
                Application.OpenForms["frm_NhanVien"].Activate();
                Application.OpenForms["frm_NhanVien"].Width= 663;
                Application.OpenForms["frm_NhanVien"].Height = 246;
                Application.OpenForms["frm_NhanVien"].Show();
            }

        }
        string chucvu = "";
        private void timer_Gio_Tick(object sender, EventArgs e)
        {
            chucvu = radLabelElement4.Text;
            PhanQuyen(chucvu);
            Frm_DangNhap dn = Frm_DangNhap.TaoDoiTuong();
            radLabelElement6.Text = DateTime.Now.Hour.ToString() + "giờ " + DateTime.Now.Minute.ToString() + "phút " + DateTime.Now.Second.ToString()+"giây";
            if (dn.kt_frmDangNhap == 1 || dn.kt_frmDangNhap == 2)
            {
                radLabelElement2.Text = dn.ten_NhanVien_DangNhap;
                radLabelElement4.Text = dn.chucvu_NhanVien;
                MaNV_frmMain = dn.MaNV_DangNhap;
                radRibbonBarGroup1.Text = "Đăng Xuất";
                radButtonElement1.Image = imageList1.Images[1];
            }
        }
        private void radButtonElement8_Click_1(object sender, EventArgs e)
        {
            if (Application.OpenForms["frm_QuanLiChiTieu"] == null)
            {
                frm_QuanLiChiTieu qlct = frm_QuanLiChiTieu.TaoDoiTuong();
                qlct.MdiParent = this;
                qlct.Show();
            }
            else
            {
                Application.OpenForms["frm_QuanLiChiTieu"].Activate();
                Application.OpenForms["frm_QuanLiChiTieu"].Show();
            }
        }
        public void PhanQuyen(string chucvu)
        {
            if (!(chucvu == "Quản Lý Hệ Thống"))
            {                                
                if (!(chucvu == ""))
                {
                    linkLabel1.Visible = true;
                    switch (chucvu)
                    {
                        case "Nhân Viên Giao":
                            //
                            radButtonElement2.Enabled = false;
                            radButtonElement3.Enabled = false;
                            radButtonElement4.Enabled = true;
                            radButtonElement5.Enabled = false;
                            radButtonElement6.Enabled = false;
                            radButtonElement7.Enabled = false;
                            radButtonElement8.Enabled = true;
                            radButtonElement10.Enabled = true;
                            radButtonElement11.Enabled = true;
                            break;

                        case "Nhân Viên Nhập":
                            //
                            radButtonElement2.Enabled = false;
                            radButtonElement3.Enabled = true;
                            radButtonElement4.Enabled = false;
                            radButtonElement5.Enabled = true;
                            radButtonElement6.Enabled = false;
                            radButtonElement7.Enabled = false;
                            radButtonElement8.Enabled = true;
                            radButtonElement10.Enabled = true;
                            radButtonElement11.Enabled = true;
                            break;

                        case "Quản Lý":
                            //
                            radButtonElement2.Enabled = true;
                            radButtonElement3.Enabled = false;
                            radButtonElement4.Enabled = false;
                            radButtonElement5.Enabled = false;
                            radButtonElement6.Enabled = true;
                            radButtonElement7.Enabled = true;
                            radButtonElement8.Enabled = true;
                            radButtonElement10.Enabled = true;
                            radButtonElement11.Enabled = true;
                            break;
                    }

                }
                else
                {
                    linkLabel1.Visible = false;
                    radButtonElement2.Enabled = false;
                    radButtonElement3.Enabled = false;
                    radButtonElement4.Enabled = false;
                    radButtonElement5.Enabled = false;
                    radButtonElement6.Enabled = false;
                    radButtonElement7.Enabled = false;
                    radButtonElement8.Enabled = false;
                    radButtonElement10.Enabled = false;
                    radButtonElement11.Enabled = false;
                }
            }
            else
            {
                radButtonElement2.Enabled = true;
                radButtonElement3.Enabled = true;
                radButtonElement4.Enabled = true;
                radButtonElement5.Enabled = true;
                radButtonElement6.Enabled = true;
                radButtonElement7.Enabled = true;
                radButtonElement8.Enabled = true;
                radButtonElement10.Enabled = true;
                radButtonElement11.Enabled = true;
                linkLabel1.Visible = true;
            }
        }       
    }
}
